package com.gpframework.module.controller.module;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.module.domain.GpAccountManagement;
import com.gpframework.module.module.domain.GpDomesticFreightForwarding;
import com.gpframework.module.module.domain.req.GpDomesticFreightForwardingAdd;
import com.gpframework.module.module.domain.req.GpDomesticFreightForwardingReq;
import com.gpframework.module.module.domain.req.GpDomesticFreightForwardingUpdate;
import com.gpframework.module.module.domain.vo.GpCustomerManagementVo;
import com.gpframework.module.module.domain.vo.GpDomesticFreightForwardingExcel;
import com.gpframework.module.module.domain.vo.GpDomesticFreightForwardingVo;
import com.gpframework.module.module.service.IGpAccountManagementService;
import com.gpframework.module.module.service.IGpDomesticFreightForwardingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GpDomesticFreightForwardingController
 * @Author gpframework
 * @Date 2024-01-08
 * @Version 1.0.0
 * @Description 国内合作货代理管理Controller
 */
@Validated
@Api(tags = "国内合作货代理管理")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/module/domesticfreightforwarding" )
public class GpDomesticFreightForwardingController extends BaseController {

    private final IGpDomesticFreightForwardingService iGpDomesticFreightForwardingService;
    private final IGpAccountManagementService iGpAccountManagementService;


 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-01-08
  * @Param gpDomesticFreightForwarding
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('module:domesticfreightforwarding:list')")
    @GetMapping("/list")
    public TableDataInfo<GpDomesticFreightForwardingVo> list(GpDomesticFreightForwardingReq gpDomesticFreightForwardingReq)
    {
        startPage();
        QueryWrapper<GpDomesticFreightForwarding> lqw = new QueryWrapper<GpDomesticFreightForwarding>();
        gpDomesticFreightForwardingReq.generatorQuery(lqw,true);
        List<GpDomesticFreightForwardingVo> list = iGpDomesticFreightForwardingService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpDomesticFreightForwarding
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('module:domesticfreightforwarding:add')" )
    @Log(title = "添加国内合作货代理管理" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpDomesticFreightForwardingAdd gpDomesticFreightForwardingAdd) {
        GpDomesticFreightForwarding gpDomesticFreightForwarding = Convert.convert(new TypeReference<GpDomesticFreightForwarding>() {}, gpDomesticFreightForwardingAdd);
        return toAjax(iGpDomesticFreightForwardingService.save(gpDomesticFreightForwarding) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpDomesticFreightForwarding
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('module:domesticfreightforwarding:edit')" )
    @Log(title = "修改国内合作货代理管理" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpDomesticFreightForwardingUpdate gpDomesticFreightForwardingUpdate) {
        GpDomesticFreightForwarding gpDomesticFreightForwarding = Convert.convert(new TypeReference<GpDomesticFreightForwarding>() {}, gpDomesticFreightForwardingUpdate);
        return toAjax(iGpDomesticFreightForwardingService.updateById(gpDomesticFreightForwarding) ? 1 : 0);
    }


    /**
     * @Description 导出国内合作货代理管理模板
     * @Author gpframework
     * @Date 2023-05-24
     * @Param gpCustomer
     * @Return TableDataInfo
     */
    @ApiOperation("导出国内合作货代理管理模板")
    @Log(title = "导出国内合作货代理管理模板" , businessType = BusinessType.EXPORT)
    @GetMapping("/importTemplate" )
    public AjaxResult exportTemplate() {
        ExcelUtil<GpDomesticFreightForwardingExcel> util = new ExcelUtil<>(GpDomesticFreightForwardingExcel.class);
        return util.exportExcel(GpDomesticFreightForwardingExcel.list(), "模板");
    }

    /**
     * @Description 导入国内合作货代理管理
     * @Author gpframework
     * @Date 2023-10-27
     * @Param (GpMember
     * @Return TableDataInfo
     */
    @Log(title = "导入国内合作货代理管理" , businessType = BusinessType.IMPORT)
    @PostMapping("/import" )
    @ApiOperation("导入国内合作货代理管理")
    public AjaxResult entityImport(MultipartFile file, Long brandId, Long storeId) throws Exception {
        ExcelUtil<GpDomesticFreightForwardingExcel> util = new ExcelUtil<GpDomesticFreightForwardingExcel>(GpDomesticFreightForwardingExcel. class);
        List<GpDomesticFreightForwardingExcel> excelList = util.importExcel(file.getInputStream());

        return AjaxResult.builder(() -> {
            return this.iGpDomesticFreightForwardingService.gpDomesticFreightForwardingImport(excelList, brandId, storeId);
        });
    }

    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpDomesticFreightForwarding
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('module:domesticfreightforwarding:export')" )
    @Log(title = "导出国内合作货代理管理" , businessType = BusinessType.EXPORT)
    @ApiOperation("导出国内合作货代理管理")
    @GetMapping("/export" )
    public AjaxResult export(GpDomesticFreightForwardingReq gpDomesticFreightForwardingReq) {
        QueryWrapper<GpDomesticFreightForwarding> lqw = new QueryWrapper<GpDomesticFreightForwarding>();
        gpDomesticFreightForwardingReq.generatorQuery(lqw,true);
        List<GpDomesticFreightForwardingVo> list = iGpDomesticFreightForwardingService.listVo(lqw);
        ExcelUtil<GpDomesticFreightForwardingVo> util = new ExcelUtil<GpDomesticFreightForwardingVo>(GpDomesticFreightForwardingVo. class);
        return util.exportExcel(list, "domesticfreightforwarding" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpDomesticFreightForwarding
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('module:domesticfreightforwarding:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpDomesticFreightForwardingVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpDomesticFreightForwardingService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpDomesticFreightForwarding
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('module:domesticfreightforwarding:remove')" )
    @Log(title = "删除国内合作货代理管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpDomesticFreightForwardingService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }



    /**
     * @Description 绑定账户
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpCustomerManagement
     * @Return TableDataInfo
     */
    @PreAuthorize("@ss.hasPermi('module:customer:bindAccount')" )
    @PutMapping(value = "/bindAccount" )
    public AjaxResult<GpCustomerManagementVo> bindAccount(@Valid @RequestBody GpDomesticFreightForwardingUpdate gpDomesticFreightForwardingUpdate) {
        if (gpDomesticFreightForwardingUpdate.getAccountId()==null && gpDomesticFreightForwardingUpdate.getId()==null){
            return AjaxResult.parameterError();
        }
        GpAccountManagement gpAccountManagement = iGpAccountManagementService.getById(gpDomesticFreightForwardingUpdate.getAccountId());
        if (gpAccountManagement==null){
            return AjaxResult.error("绑定的账户不存在");
        }
        GpDomesticFreightForwarding gpDomesticFreightForwarding = iGpDomesticFreightForwardingService.getById(gpDomesticFreightForwardingUpdate.getId());

        gpDomesticFreightForwarding.setAccountId(gpAccountManagement.getId());
        gpDomesticFreightForwarding.setBindingFlag("1");

        iGpDomesticFreightForwardingService.updateById(gpDomesticFreightForwarding);
        return AjaxResult.success();
    }


    /**
     * @Description 解绑绑定账户
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpCustomerManagement
     * @Return TableDataInfo
     */
    @PreAuthorize("@ss.hasPermi('module:customer:unbindAccount')" )
    @PutMapping(value = "/unbindAccount" )
    public AjaxResult<GpCustomerManagementVo> unbindAccount(@Valid @RequestBody GpDomesticFreightForwardingUpdate gpDomesticFreightForwardingUpdate) {
        if (gpDomesticFreightForwardingUpdate.getId()==null){
            return AjaxResult.parameterError();
        }
        GpDomesticFreightForwarding gpDomesticFreightForwarding = iGpDomesticFreightForwardingService.getById(gpDomesticFreightForwardingUpdate.getId());
        if (gpDomesticFreightForwarding!=null){
            iGpDomesticFreightForwardingService.update(new UpdateWrapper<GpDomesticFreightForwarding>().set("account_id",null));
            iGpDomesticFreightForwardingService.update(new UpdateWrapper<GpDomesticFreightForwarding>().set("binding_flag",0));

        }

        return AjaxResult.success();
    }

}
